package com.example.shoppingcenter.Controller;

import com.example.shoppingcenter.Config.XFConfig;
import com.example.shoppingcenter.POJO.Messages;
import com.example.shoppingcenter.POJO.Result;
import com.example.shoppingcenter.Service.xfService;
import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/model")
public class xfController {

    @Resource
    private xfService xfService;
    private static boolean totalFlag = true;
    @Resource
    private XFConfig xfConfig;

    @RequestMapping("/translation")
    public ResponseEntity<Messages> getTranslation(String usermessage) throws Exception {
        System.out.println("translation :"+usermessage);
        Messages ok = xfService.getAiMessage(usermessage,-1);
        return ResponseEntity.ok(ok);
    }

    @RequestMapping("/getmessage")
    @ResponseBody
    public ResponseEntity<Messages> getAiMessage(Integer bro, String userContent, Integer chatId,Integer changedMessageId ,Integer lastMessageId) throws Exception {
        System.out.println("Controller正常");
        Messages ok = xfService.getAiMessage(userContent,lastMessageId);
        if (bro == 0) {
            xfService.saveSonMessage(chatId, userContent, lastMessageId, ok.getModelContent());
        } else if (bro == 1) {
            xfService.saveBroMessage(chatId, userContent,changedMessageId, lastMessageId, ok.getModelContent());
        }
        return ResponseEntity.ok(ok);
    }

//    @RequestMapping("/getHistoryMessage")
//    @ResponseBody
//    public ResponseEntity<Messages> getHistoryMessage(HttpServletRequest httpServletRequest, String usermessage, Integer chatId) throws Exception {
//        System.out.println("Controller正常");
//        Messages ok = xfService.getAiMessage(usermessage);
//        xfService.saveMessage(chatId, usermessage, ok.getModelContent());
//        return ResponseEntity.ok(ok);
//    }

    @RequestMapping("/deleteChat")
    @ResponseBody
    public ResponseEntity<Result> deleteChat(HttpServletRequest httpServletRequest, Integer chatId) throws JsonProcessingException {
        System.out.println("删除对话Chat");
        return ResponseEntity.ok(xfService.deleteChat(chatId));
    }

    @RequestMapping("/createChat")
    @ResponseBody
    public ResponseEntity<Result> createChat(HttpServletRequest httpServletRequest, Integer userId) throws JsonProcessingException {
        System.out.println("创建新对话Chat");
        return ResponseEntity.ok(xfService.createChat(userId));
    }

    @RequestMapping("/reloadMessages")
    @ResponseBody
    public ResponseEntity<Result> reloadMessages(HttpServletRequest httpServletRequest, Integer chatId,Integer indexId, Model model) {
        Result result = xfService.reloadMessagesList(chatId,indexId);
        model.addAttribute("messagesList", result.getMessage());
        return ResponseEntity.ok(result);
    }

    @RequestMapping("/updateChatLastMessages")
    @ResponseBody
    public ResponseEntity<Result> updateChatLastMessages(HttpServletRequest httpServletRequest, Integer chatId,Integer preId,Integer nextId,Integer lastMessageId, Model model) {
        Result result = xfService.updateChatLastMessages(chatId,preId,nextId,lastMessageId);
        return ResponseEntity.ok(result);
    }
}
